﻿using BySoft.Server.Common;
using BySoft.Server.Common.Entity;
using OneSoft.DataAccess;
using OneSoft.EntityCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace BySoft.Server.DataAccess
{
    public partial class BsKnifeinstockDA
    {
        public static DataTable SelectList(string strBillID,string strKnifeID, string strSupplierID, string strFrom, string strEnd, int pageIndex, int PageSize, out int m_count)
        {
            try
            {
                string strcmd = @"SELECT a.*,
  
       d.Nums, d.Price, d.Amount,
       ms.SupplierName,
       d.KnifeID,
       m.KnifeName,
       m.Specifications,
       m.Unit,
       m.BelongCustomer, m.Factory, m.SafeStock,
       mt.KnifeTypeName 
FROM BS_KnifeInStock AS a
   LEFT JOIN  MD_Supplier AS ms ON  ms.SupplierID = a.SupplierID
   LEFT JOIN Bs_KnifeInStockDetail d
		    ON d.BillID=a.BillID
   LEFT JOIN md_Knife m
            ON  d.KnifeID = m.KnifeID
       LEFT JOIN md_KnifeType mt
            ON  m.KnifeTypeID = mt.KnifeTypeID
  WHERE a.[Status]='1' ";

                DataAccessParameterCollection dpc = new DataAccessParameterCollection();
                if (!string.IsNullOrEmpty(strBillID))
                {
                    strcmd += " and a.BillID like @strBillID";
                    dpc.AddWithValue("@strBillID", "%" + strBillID + "%");
                }

                if (!string.IsNullOrEmpty(strKnifeID))
                {
                    strcmd += " and d.KnifeID like @strKnifeID";
                    dpc.AddWithValue("@strKnifeID", "%" + strKnifeID + "%");
                }
                if (!string.IsNullOrEmpty(strSupplierID))
                {
                    strcmd += " and a.SupplierID = @strSupplierID";
                    dpc.AddWithValue("@strSupplierID",  strSupplierID );
                }

                if (!string.IsNullOrEmpty(strFrom))
                {
                    strcmd += " and a.BillDate >= @strFrom";
                    dpc.AddWithValue("@strFrom", strFrom);
                }
                if (!string.IsNullOrEmpty(strEnd))
                {
                    strcmd += " and a.BillDate <= @strEnd";
                    dpc.AddWithValue("@strEnd", strEnd );
                }
                strcmd += " order by a.BillDate desc";



                DataTable  dt = DataAccessCommon.ExecuteDataTablePaging(strcmd, pageIndex, PageSize, dpc, out m_count);
         
               
               return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

     

      
  
       

      


      


      
        #region View Data
        public static DataTable SelectHeadData(string strBillID)
        {
            try
            {
                string strcmd = @"SELECT a.*,s.supplierName ,                           
    
-1 as IsNew,-1 as IsDel
from Bs_KnifeInStock a
  
LEFT JOIN md_supplier s   on a.SupplierID =s.SupplierID
 
where a.BillID=@BillID";

                DataAccessParameterCollection dpc = new DataAccessParameterCollection();

                dpc.AddWithValue("@BillID", strBillID);
                DataTable dt= DataAccessCommon.Select(strcmd, dpc).Tables[0];
                return  dt ;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public static DataTable SelectDetailData(string strBillID)
        {
            try
            {
                string strcmd = @"SELECT a.*,
       m.KnifeName,
       m.Specifications,
       m.Unit,
       m.BelongCustomer, m.Factory, m.SafeStock,
       mt.KnifeTypeName,
       -1  AS IsNew,
       -1  AS IsDel
FROM   Bs_KnifeInStockDetail a
       LEFT JOIN md_Knife m
            ON  a.KnifeID = m.KnifeID
       LEFT JOIN md_KnifeType mt
            ON  m.KnifeTypeID = mt.KnifeTypeID
where a.BillID=@BillID";

                DataAccessParameterCollection dpc = new DataAccessParameterCollection();

                dpc.AddWithValue("@BillID", strBillID);
                return DataAccessCommon.Select(strcmd, dpc).Tables[0];
            }
            catch (Exception ex)
            {
                throw ex;
            }

        } 
        #endregion



        #region AUD
        public static bool SaveOrUpdate(BsKnifeinstockEntity entity, DataTable KnifeinstockDetailDT, bool isNew)
        {
            bool istag = true;
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                broker.BeginTransaction();
                try
                {
                    if (isNew)
                    {
                        DataAccessCommon.Insert(entity, broker);
                    }
                    else
                    {
                        DataAccessCommon.UpdateOnly(entity, broker);
                        ClsCommDA.Del("BS_KnifeInStockDetail", "BillID",entity.BillID, broker);//delete  first  & then insert new 
                     

                    }
                    if (KnifeinstockDetailDT != null && KnifeinstockDetailDT.Rows.Count > 0)
                    {
                        foreach (DataRow drItem in KnifeinstockDetailDT.Rows)
                        {
                            BsKnifeinstockdetailEntity DetailEntity = new BsKnifeinstockdetailEntity();
                            DetailEntity.BillID = entity.BillID;
                            DetailEntity.KnifeID = CommonMethod.GetString(drItem["KnifeID"]);
                            DetailEntity.Nums = CommonMethod.ConvertToDecimal(drItem["Nums"]);
                            DetailEntity.Price = CommonMethod.ConvertToDecimal(drItem["Price"]);
                            DetailEntity.Amount = CommonMethod.ConvertToDecimal(drItem["Amount"]);
                        
                     
                            string strIsNew = CommonMethod.GetString(drItem["IsNew"]);
                            string strIsDel = CommonMethod.GetString(drItem["IsDel"]);
                            if (strIsNew == "1")//insert
                            {
                                DataAccessCommon.Insert(DetailEntity, true, broker);
                            }
                            else
                            {
                                if ( strIsDel != "1")//update
                                {
                                    //DetailEntity.DetailID = CommonMethod.ConvertToDecimal(drItem["DetailID"]);
                                    //DataAccessCommon.UpdateOnly(DetailEntity, broker);
                                    DataAccessCommon.Insert(DetailEntity, true, broker);
                                
                                }
                                else if ( strIsDel == "1")
                                {
                                    //删除明细  不会用到这里，@See Del()
                                    ClsCommDA.Del("BS_KnifeInStockDetail", "DetailID", CommonMethod.GetString(drItem["DetailID"]), broker);
                                }
                               

                            }
                           
                        }
                    }
                    broker.Commit();
                }
                catch
                {
                    istag = false;
                    broker.Rollback();
                    throw;
                }
                return istag;
            }
        }

        public static bool Del(string strCode)
        {
            bool isResult = true;
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    DataAccessCommon.Delete(strCode, "BillID", "BS_KnifeinstockDetail");
                    DataAccessCommon.Delete(strCode, "BillID", "BS_Knifeinstock");
                    broker.Commit();
                }
                catch (Exception ex)
                {
                    isResult = false;
                    broker.Rollback();
                    throw ex;
                }
            }
            return isResult;
        } 
        #endregion
    }
}
